﻿namespace PortableRiotGames.Infrastructure.Converters
{
    using Newtonsoft.Json;
    using Newtonsoft.Json.Linq;
    using PortableRiotGames.Interfaces.League;
    using System;
    using System.Collections.Generic;

    public class JsonGetLeaguesResponseConverter : JsonCustomConverter<GetLeaguesResponse>
    {
        protected override GetLeaguesResponse Create(Type objectType, JContainer container)
        {
            var response = new GetLeaguesResponse() { Teams = new List<GetLeaguesResponseTeam>() };

            foreach (var token in container as JObject)
            {
                var team = token.Value.ToObject<GetLeaguesResponseTeam>();
                team.TeamID = token.Key;
                response.Teams.Add(team);
            }

            return response;
        }
    }
}
